package de.matusystems.rest

import grails.converters.JSON
import groovyx.net.http.*

class StorageWebService {

	private String serviceURL = System.getenv()['CAPP_STORAGE_SERVICE']

	def saveFile(String filename, String contentType, byte[] file) {

		HTTPBuilder http = new HTTPBuilder(serviceURL+"/file")

		def jsonResponse

		def fileAsBase64 = file.encodeBase64().toString()

		http.request(Method.POST, groovyx.net.http.ContentType.JSON) { req ->
			body = [filename : filename, contentType : contentType, file : fileAsBase64]
			response.success = { resp, json ->
				jsonResponse = json
			}
		}

		return jsonResponse as JSON
	}
}
